// package com.example.utils;

// import com.example.model.entity.Order;
// import com.example.model.entity.User;
// import com.example.model.vo.SatisfactionScore;
// import com.example.model.vo.SatisfactionScoresVo;
// import lombok.AllArgsConstructor;
// import org.springframework.stereotype.Component;

// import java.util.*;
// import java.util.stream.Collectors;
// import java.util.stream.IntStream;

// @Component
// public class RecommendUtil {
//     /**
//      * 在给定username的情况下，计算其他用户和它的距离并排序
//      * @param userId
//      * @return
//      */
//     private Map<Double, Integer> computeNearestNeighbor(Integer userId, List<SatisfactionScoresVo> satisfactionScoresVos) {
//         Map<Double, Integer> distances = new TreeMap<>();

//         SatisfactionScoresVo u1 = new SatisfactionScoresVo(userId, null);
//         for (SatisfactionScoresVo sVo :satisfactionScoresVos) {
//             if (sVo.getUserId().equals(userId)) {
//                 u1 = sVo;
//             }
//         }

//         for (int i = 0; i < satisfactionScoresVos.size(); i++) {
//             SatisfactionScoresVo u2 = satisfactionScoresVos.get(i);

//             if (!u2.getUserId().equals(u1.getUserId())) {
//                 double distance = pearson_dis(u2.getSatisfactionScores(), u1.getSatisfactionScores());
//                 distances.put(distance, u2.getUserId());
//             }

//         }
//         System.out.println("该用户与其他用户的皮尔森相关系数 -> " + distances);
//         return distances;
//     }


//     /**
//      * 计算2个打分序列间的pearson距离
//      * 选择公式四进行计算
//      * @param rating1
//      * @param rating2
//      * @return
//      */
//     private double pearson_dis(List<SatisfactionScore> rating1, List<SatisfactionScore> rating2) {
//         int n=rating1.size();
//         List<Integer> rating1ScoreCollect = rating1.stream().map(SatisfactionScore::getScore).collect(Collectors.toList());
//         List<Integer> rating2ScoreCollect = rating2.stream().map(SatisfactionScore::getScore).collect(Collectors.toList());

//         double Ex= rating1ScoreCollect.stream().mapToDouble(x->x).sum();
//         double Ey= rating2ScoreCollect.stream().mapToDouble(y->y).sum();
//         double Ex2=rating1ScoreCollect.stream().mapToDouble(x->Math.pow(x,2)).sum();
//         double Ey2=rating2ScoreCollect.stream().mapToDouble(y->Math.pow(y,2)).sum();
//         double Exy= IntStream.range(0,n).mapToDouble(i->rating1ScoreCollect.get(i)*rating2ScoreCollect.get(i)).sum();
//         double numerator=Exy-Ex*Ey/n;
//         double denominator=Math.sqrt((Ex2-Math.pow(Ex,2)/n)*(Ey2-Math.pow(Ey,2)/n));
//         if (denominator==0) {
//             return 0.0;
//         }
//         return numerator/denominator;
//     }


//     public List<SatisfactionScore> recommend(Integer userId, List<SatisfactionScoresVo> satisfactionScoresVos) {
//         //找到最近邻
//         Map<Double, Integer> distances = computeNearestNeighbor(userId, satisfactionScoresVos);
//         Integer nearest = distances.values().iterator().next();
//         System.out.println("最近邻 -> " + nearest);

//         SatisfactionScoresVo neighborRatings = new SatisfactionScoresVo();
//         for (SatisfactionScoresVo satisfactionScoresVo:satisfactionScoresVos) {
//             if (nearest.equals(satisfactionScoresVo.getUserId())) {
//                 neighborRatings = satisfactionScoresVo;
//             }
//         }
//         // System.out.println("最近邻看过的电影 -> " + neighborRatings.getSatisfactionScores());

//         SatisfactionScoresVo userRatings = new SatisfactionScoresVo();
//         for (SatisfactionScoresVo satisfactionScoresVo:satisfactionScoresVos) {
//             if (userId.equals(satisfactionScoresVo.getUserId())) {
//                 userRatings = satisfactionScoresVo;
//             }
//         }
//         // System.out.println("用户看过的电影 -> " + userRatings.getSatisfactionScores());

//         List<SatisfactionScore> recommendationEmployees = new ArrayList<>();
//         for (SatisfactionScore satisfactionScore : neighborRatings.getSatisfactionScores()) {
//             if (!userRatings.find(satisfactionScore.getEmployeeId())) {
//                 recommendationEmployees.add(satisfactionScore);
//             }
//         }
//         recommendationEmployees.sort(Comparator.comparingInt(SatisfactionScore::getScore).reversed());
//         return recommendationEmployees;
//     }
// }